<template>
  <el-dialog top="7vh"
             :title="titleShow" :close-on-click-modal="false" :visible.sync="visible"
             @close="clearDataList()" width="1200px">
    <basic-container>
      <div style="height: 60vh; overflow-y: auto;">
        <el-row>
          <el-col :span="24">
            <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="140px">
              <el-row>
                <el-col :span="24">
                  <el-form-item label="个人考核分级" prop="config03">
                    <el-select :clearable="true" v-model="dataForm.config03" placeholder="请选择"  @change="selectchangedkhfj"  class="xiaLa"
                               style="width:200px">
                      <el-option v-for="(item,index) in lxOptions" :key="index" :label="item.label" :value="item.value">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="工时(工作量)奖励" prop="config01">
                    <el-input-number v-model="dataForm.config01" :precision="2" :step="1" :min="0"
                                     style="width:200px;"></el-input-number>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="员工考核基础分">
                    <el-input-number v-model="dataForm.config02" :precision="2" :step="1" :min="0"
                                     style="width:200px;"></el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="员工月绩效考核分">
                    <el-input-number v-model="dataForm.config04" :precision="2" :step="1" :min="0"
                                     style="width:200px;"></el-input-number>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="绩效年薪基数">
                    <el-input-number v-model="dataForm.config05" :precision="2" :step="1" :min="0"
                                     style="width:200px;"></el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="综合业绩分值">
                    <el-input-number v-model="dataForm.config06" :precision="2" :step="1" :min="0"
                                     style="width:200px;"></el-input-number>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="绩效奖金基数">
                    <el-input-number v-model="dataForm.config07" :precision="2" :step="1" :min="0"
                                     style="width:200px;"></el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="个人绩效分数">
                    <el-input-number v-model="dataForm.config08" :precision="2" :step="1" :min="0"
                                     style="width:200px;"></el-input-number>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="绩效分值指数">
                    <el-input-number v-model="dataForm.config09" :precision="2" :step="1" :min="0"
                                     style="width:200px;"></el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-table :data="jjxData" stripe border width="99%">
              <el-table-column prop="" label="序号" header-align="center" align="center" :formatter="nShowIndex"
                               width="70px"></el-table-column>
              <el-table-column prop="jjxName" header-align="center" align="center" label="奖金项名称"></el-table-column>
              <el-table-column prop="value" header-align="center" align="center" label="员工岗位系数">
                <template slot-scope="scope">
                  <el-input-number v-model="scope.row.p11" :precision="2" :step="0.1" :min="0"
                                   style="width:150px;"></el-input-number>
                </template>
              </el-table-column>
              <el-table-column header-align="center" align="center" label="员工月奖基数">
                <template slot-scope="scope">
                  <el-input-number v-model="scope.row.p12" :step="100" :min="0" style="width:150px;"></el-input-number>
                </template>
              </el-table-column>
              <el-table-column header-align="center" align="center" label="员工年奖基数">
                <template slot-scope="scope">
                  <el-input-number v-model="scope.row.p14" :step="1000" :min="0" style="width:150px;"></el-input-number>
                </template>
              </el-table-column>
              <el-table-column header-align="center" align="center" label="奖励标准奖金">
                <template slot-scope="scope">
                  <el-input-number v-model="scope.row.p13" :step="100" :min="0" style="width:150px;"></el-input-number>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
      </div>
    </basic-container>
    <span slot="footer" class="dialog-footer">
      <el-button @click="btnDefault" type="primary" plain>恢复默认</el-button>
      <el-button @click="btnZero" type="danger" plain>清零</el-button>
      <el-button type="primary" @click="btnConfirm()" v-if="canSubmit">保存</el-button>
      <el-button @click="clearDataList" type="primary" plain>关闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {getRyxxbByCode, allocationRyxxb,getRyxxbListByConfig} from '@/api/jiangjin/jiangjinryxxb'
import {getJjDeptJjx} from '@/api/jiangjin/jiangjinjjxxxb'

export default {
  data() {
    return {
      visible: false,
      canSubmit: false,
      deptId: '',
      rycodeinfo:'',
      jjxData: [],
      titleShow: '',
      ryCodes: [],

      dataForm: {
        id: '',
        config01: '',
        config02: '',
        config03: '',
        config04: '',
        config05: '',
        config06: '',
        config07: '',
        config08: '',
        config09: '',

        config11: '',
        config12: '',
        config13: '',
      },
      dataRule: {
        config03: [
          {required: true, message: '请选择个人考核分级', trigger: 'blur'}
        ],
      },
      lxOptions: [{value: '默认',label: '默认' },{value: 'A', label: 'A'}, {value: 'B', label: 'B'},
        {value: 'C', label: 'C'}, {value: 'D', label: 'D'},
        // {value: 'A1', label: 'A1'},{value: 'B1', label: 'B1'},
        // {value: 'C1', label: 'C1'},{value: 'D1', label: 'D1'},{value: 'A2', label: 'A2'},{value: 'B2', label: 'B2'}
        // ,{value: 'C2', label: 'C2'},{value: 'D2', label: 'D2'}
        ],
    }
  },
  methods: {

    /* 1、配置中默认系数、指数为1
       2、配置中默认分值为100
       3、配置中默认基数为1000
       4、其他数设置1000
       */
    btnDefault() {
      // this.dataForm.config03 = "A";

      this.dataForm.config01 = 1000;
      this.dataForm.config02 = 100;
      this.dataForm.config04 = 100;
      this.dataForm.config05 = 1000;

      this.dataForm.config06 = 100;
      this.dataForm.config07 = 1000;
      this.dataForm.config08 = 100;
      this.dataForm.config09 = 1;

      for (let item of this.jjxData) {
        item.p11 = 1;
        item.p12 = 1000;
        item.p13 = 1000;
        item.p14 = 10000;
      }
    },
    selectchangedkhfj(value){

    this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()

        getRyxxbListByConfig(this.rycodeinfo,value).then(response => {
          this.dataForm = response.data.data;
          console.log(this.dataForm)
           getJjDeptJjx(this.deptId).then(res => {
            const xData = res.data.data;
            this.jjxData = [];
            this.jjxData.push({
              jjxId: 0,
              jjxName: "默认",
              p11: this.setConfig(0, 11),//'',
              p12: this.setConfig(0, 12),//'',
              p13: this.setConfig(0, 13),//'',
              p14: this.setConfig(0, 14),//'',
              value: ''
            })

            xData.map(item => {
              this.jjxData.push({
                jjxId: item.id,
                jjxName: item.jjxMc,
                p11: this.setConfig(item.id, 11),//'',
                p12: this.setConfig(item.id, 12),//'',
                p13: this.setConfig(item.id, 13),//'',
                p14: this.setConfig(item.id, 14),//'',
                value: ''
              })
            })
          })


        })
      })
    },
    //清零
    btnZero() {
      // this.dataForm.config03 = '';

      this.dataForm.config01 = '';
      this.dataForm.config02 = '';
      this.dataForm.config04 = '';
      this.dataForm.config05 = '';

      this.dataForm.config06 = '';
      this.dataForm.config07 = '';
      this.dataForm.config08 = '';
      this.dataForm.config09 = '';


      for (let item of this.jjxData) {
        item.p11 = '';
        item.p12 = '';
        item.p13 = '';
        item.p14 = '';
      }
    },

    // 单个人员系数设置
    init(code, deptId) {
      this.ryCodes = [];
      this.ryCodes.push(code);
      this.deptId = deptId;
      this.rycodeinfo=code;
      this.visible = true;
      this.canSubmit = true;

      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()

        getRyxxbByCode(this.ryCodes[0]).then(response => {
          this.dataForm = response.data.data;
          this.titleShow = this.dataForm.ryXm + "的系数配置";

          getJjDeptJjx(deptId).then(res => {
            const xData = res.data.data;
            this.jjxData = [];
            this.jjxData.push({
              jjxId: 0,
              jjxName: "默认",
              p11: this.setConfig(0, 11),//'',
              p12: this.setConfig(0, 12),//'',
              p13: this.setConfig(0, 13),//'',
              p14: this.setConfig(0, 14),//'',
              value: ''
            })

            xData.map(item => {
              this.jjxData.push({
                jjxId: item.id,
                jjxName: item.jjxMc,
                p11: this.setConfig(item.id, 11),//'',
                p12: this.setConfig(item.id, 12),//'',
                p13: this.setConfig(item.id, 13),//'',
                p14: this.setConfig(item.id, 14),//'',
                value: ''
              })
            })
          })

        })
      })
    },

    // 相同考核等级人员-批量系数设置
    initBatch(codes, level, deptId) {
      this.ryCodes = codes;
      this.deptId = deptId;

      this.visible = true;
      this.canSubmit = true;

      this.$nextTick(() => {
        // 获取系数配置缺省对象结构，用于批量系数设置初始化
        getRyxxbByCode('*').then(response => {
          this.dataForm = response.data.data
          this.dataForm.config03 = level //初始化考核等级
          this.titleShow = '分级统一系数配置'

          getJjDeptJjx(deptId).then(res => {
            const xData = res.data.data;
            this.jjxData = [];
            this.jjxData.push({
              jjxId: 0,
              jjxName: "默认",
              p11: this.setConfig(0, 11),//'',
              p12: this.setConfig(0, 12),//'',
              p13: this.setConfig(0, 13),//'',
              p14: this.setConfig(0, 14),//'',
              value: ''
            })

            xData.map(item => {
              this.jjxData.push({
                jjxId: item.id,
                jjxName: item.jjxMc,
                p11: this.setConfig(item.id, 11),//'',
                p12: this.setConfig(item.id, 12),//'',
                p13: this.setConfig(item.id, 13),//'',
                p14: this.setConfig(item.id, 14),//'',
                value: ''
              })
            })
          })
        })
      })
    },

    setConfig(id, vm) {
      let lTemp = [];
      let lResult = "";

      if (vm === 11 && this.dataForm.config11 != null) { //员工岗位系数
        lTemp = JSON.parse(this.dataForm.config11);
      } else if (vm === 12 && this.dataForm.config12 != null) {//每月员工奖金
        lTemp = JSON.parse(this.dataForm.config12);
      } else if (vm === 13 && this.dataForm.config13 != null) {//奖励标准奖励
        lTemp = JSON.parse(this.dataForm.config13);
      } else if (vm === 14 && this.dataForm.config14 != null) {//奖励标准奖励
        lTemp = JSON.parse(this.dataForm.config14);

      }

      for (let i = 0; i < lTemp.length; i++) {
        if (lTemp[i].jjxId == id ) {
          lResult = lTemp[i].value;

          break;
        }
      }

      return lResult;
    },
    nShowIndex(row, column, cellValue, index) {
      return index + 1;
    },
    getConfig(vm) {
      let lResult = [];
      if (vm === 11) { //员工岗位系数
        this.jjxData.map(item => {
          lResult.push({
            jjxId: item.jjxId,
            jjxName: item.jjxName,
            value: item.p11.toFixed(2)
          })
        })
      } else if (vm === 12) {//每月员工奖金
        this.jjxData.map(item => {
          lResult.push({
            jjxId: item.jjxId,
            jjxName: item.jjxName,
            value: item.p12.toFixed(2)
          })
        })
      } else if (vm === 13) {//奖励标准奖励
        this.jjxData.map(item => {
          lResult.push({
            jjxId: item.jjxId,
            jjxName: item.jjxName,
            value: item.p13.toFixed(2)
          })
        })
      } else if (vm === 14) {//奖励标准奖励
        this.jjxData.map(item => {
          lResult.push({
            jjxId: item.jjxId,
            jjxName: item.jjxName,
            value: item.p14.toFixed(2)
          })
        })
      }
      return lResult;
    },
    btnConfirm() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$set(this.dataForm, "config11", JSON.stringify(this.getConfig(11)));
          this.$set(this.dataForm, "config12", JSON.stringify(this.getConfig(12)));
          this.$set(this.dataForm, "config13", JSON.stringify(this.getConfig(13)));
          this.$set(this.dataForm, "config14", JSON.stringify(this.getConfig(14)));

          this.canSubmit = false;

          this.$set(this.dataForm, "ryCodes", this.ryCodes);
          allocationRyxxb(this.dataForm).then(res => {
            this.$notify.closeAll();
            this.$notify.success('修改成功');
            this.visible = false;
            this.$emit("refresh");
            this.canSubmit = true;
          }).catch(() => {
            this.canSubmit = true;
          });

        }
      })
    },
    clearDataList() {
      this.initNone();
      this.visible = false;
    },
    //初始化
    initNone() {
      this.dataForm = {};
      this.dataForm = {
        config01: '',
        config02: '',
        config03: '',
        config04: '',
        config05: '',
        config06: '',
        config07: '',
        config08: '',
        config09: '',

        config11: '',
        config12: '',
        config13: '',
        config14: '',
      };
      this.jjxData = []
    }

  }
}
</script>

<style scoped>

</style>
